import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _isNil from "lodash/isNil";
import _toInteger from "lodash/toInteger";
import _get from "lodash/get";
import _startsWith from "lodash/startsWith";
import _isString from "lodash/isString";
import _isFunction from "lodash/isFunction";
import _min from "lodash/min";
import _isInteger from "lodash/isInteger";
import _cloneDeep from "lodash/cloneDeep";
import _forEach from "lodash/forEach";

/**
 * utils
 * @date 2018/9/28

 */
import React from 'react'; // 这个 React 是为了上传组件, 当为预览模式时,上传组件还是要显示

import moment from 'moment';
import { Input, DatePicker, InputNumber } from 'antd';
import intl from "../../../utils/intl";
import { getDateFormat } from "../../../utils/utils";
import { DEFAULT_DATE_FORMAT, DEFAULT_DATETIME_FORMAT, DEFAULT_TIME_FORMAT } from "../../../utils/constants";
import Switch from "../../Switch";
import Checkbox from "../../Checkbox";
import Lov from "../../Lov";
import ValueList from "../../ValueList";
import Upload from "../../Upload";
import { getDisplayFieldCode } from "../utils";
var TextArea = Input.TextArea; // 为了 防止 webpack 过度优化

var components = {
  DatePicker: DatePicker,
  ValueList: ValueList,
  TextArea: TextArea,
  Input: Input,
  InputNumber: InputNumber,
  Lov: Lov,
  Switch: Switch,
  Checkbox: Checkbox,
  Upload: Upload
};
var contextPrefix = 'this.';
/**
 * 合并 Descriptor 后的属性
 * @param {Object} dealProps2 - 组件属性处理后的属性
 * @param {Object} contextProps - context 属性
 */

function mergeProps(dealProps2, contextProps) {
  var dealProps3 = _cloneDeep(dealProps2);

  _forEach(contextProps, function (_, contextPropKey) {
    Object.defineProperty(dealProps3, contextPropKey, Object.getOwnPropertyDescriptor(contextProps, contextPropKey));
  });

  return dealProps3;
}
/**
 * 处理context属性 以及将 属性转为对象
 * @param {*} props - 属性
 * @param {*} context - 外面传进来的 this
 */


export function commonDealForProps(props, context) {
  var contextProps = {};
  var dealProps1 = {};

  _forEach(props, function (prop) {
    var dealProp = prop.attributeValue;

    if (_isString(dealProp) && _startsWith(dealProp, contextPrefix)) {
      var attributePath = dealProp.substr(5);
      dealProp = undefined;
      Object.defineProperty(contextProps, prop.attributeName, {
        get: function get() {
          return _get(context, attributePath);
        },
        enumerable: true
      });
    }

    if (dealProp !== undefined) {
      dealProps1[prop.attributeName] = dealProp;
    }
  });

  return {
    contextProps: contextProps,
    dealProps1: dealProps1
  };
} // TODO 数值 判断需要改一下
// 获取组件属性

export var propUtils = {
  Checkbox: getCheckboxProps,
  DatePicker: getDatePickerProps,
  DateTimePicker: getDateTimePickerProps,
  Lov: getLovProps,
  InputNumber: getInputNumberProps,
  Switch: getSwitchProps,
  TextArea: getTextAreaProps,
  Input: getInputProps,
  ValueList: getValueListProps,
  Upload: getUploadProps
};
/**
 * 获取 Checkbox 组件的属性
 * @param {Object[]} props - 属性
 * @param {Object} context - 外面传进来的 this
 */

function getCheckboxProps(props, context) {
  var _commonDealForProps = commonDealForProps(props, context),
      contextProps = _commonDealForProps.contextProps,
      dealProps1 = _commonDealForProps.dealProps1;

  return mergeProps(dealProps1, contextProps);
}
/**
 * 获取 Checkbox 组件的属性
 * @param {Object[]} props - 属性
 * @param {Object} context - 外面传进来的 this
 */


function getUploadProps(props, context) {
  var _commonDealForProps2 = commonDealForProps(props, context),
      contextProps = _commonDealForProps2.contextProps,
      dealProps1 = _commonDealForProps2.dealProps1;

  return mergeProps(dealProps1, // {
  //   // templateAttachmentUUID: `${field.investigateTemplateId}-${field.investgCfLineId}`,
  //   ...dealProps1,
  // },
  contextProps);
}
/**
 * 获取 DatePicker 组件的属性
 * @param {Object[]} props - 属性
 * @param {Object} context - 外面传进来的 this
 */


function getDatePickerProps(props, context) {
  var _commonDealForProps3 = commonDealForProps(props, context),
      contextProps = _commonDealForProps3.contextProps,
      dealProps1 = _commonDealForProps3.dealProps1;

  return mergeProps(_objectSpread({
    style: {
      width: '100%'
    },
    format: DEFAULT_DATE_FORMAT,
    placeholder: ''
  }, dealProps1), contextProps);
}
/**
 * 获取 TimePicker 组件的属性
 * @param {Object[]} props - 属性
 * @param {Object} context - 外面传进来的 this
 */


function getDateTimePickerProps(props, context) {
  var _commonDealForProps4 = commonDealForProps(props, context),
      contextProps = _commonDealForProps4.contextProps,
      dealProps1 = _commonDealForProps4.dealProps1;

  return mergeProps(_objectSpread({
    style: {
      width: '100%'
    },
    format: DEFAULT_DATETIME_FORMAT,
    placeholder: ''
  }, dealProps1), contextProps);
}
/**
 * 获取 Lov 组件的属性
 * @param {Object[]} props - 属性
 * @param {Object} context - 外面传进来的 this
 * @param {Object} field - 字段
 */


function getLovProps(props, context, field) {
  var _commonDealForProps5 = commonDealForProps(props, context),
      contextProps = _commonDealForProps5.contextProps,
      dealProps1 = _commonDealForProps5.dealProps1; // 调查表 不会出现 租户id 动态的情况


  return mergeProps(_objectSpread({
    code: field.lovCode,
    // todo 不确定租户id 到底怎么弄
    // queryParams: {
    //   organizationId: dealProps1.organizationId,
    //   tenantId: dealProps1.organizationId,
    // },
    style: {
      width: '100%'
    },
    textField: getDisplayFieldCode(field)
  }, dealProps1), contextProps);
}
/**
 * 获取 InputNumber 组件的属性
 * @param {Object[]} props - 属性
 * @param {Object} context - 外面传进来的 this
 */


function getInputNumberProps(props, context) {
  var _commonDealForProps6 = commonDealForProps(props, context),
      contextProps = _commonDealForProps6.contextProps,
      dealProps1 = _commonDealForProps6.dealProps1;

  return mergeProps(_objectSpread({
    style: {
      width: '100%'
    }
  }, dealProps1), contextProps);
}
/**
 * 获取 Switch 组件的属性
 * @param {Object[]} props - 属性
 * @param {Object} context - 外面传进来的 this
 */


function getSwitchProps(props, context) {
  var _commonDealForProps7 = commonDealForProps(props, context),
      contextProps = _commonDealForProps7.contextProps,
      dealProps1 = _commonDealForProps7.dealProps1;

  return mergeProps(dealProps1, contextProps);
}
/**
 * 获取 TextArea 组件的属性
 * @param {Object[]} props - 属性
 * @param {Object} context - 外面传进来的 this
 */


function getTextAreaProps(props, context) {
  var _commonDealForProps8 = commonDealForProps(props, context),
      contextProps = _commonDealForProps8.contextProps,
      dealProps1 = _commonDealForProps8.dealProps1;

  return mergeProps(dealProps1, contextProps);
}
/**
 * 获取 Input 组件的属性
 * @param {Object[]} props - 属性
 * @param {Object} context - 外面传进来的 this
 */


function getInputProps(props, context) {
  var _commonDealForProps9 = commonDealForProps(props, context),
      contextProps = _commonDealForProps9.contextProps,
      dealProps1 = _commonDealForProps9.dealProps1;

  return mergeProps(dealProps1, contextProps);
}
/**
 * 获取 ValueList 组件的属性
 * @param {Object[]} props - 属性
 * @param {Object} context - 外面传进来的 this
 * @param {Object} field - 字段
 */


function getValueListProps(props, context, field) {
  var _commonDealForProps10 = commonDealForProps(props, context),
      contextProps = _commonDealForProps10.contextProps,
      dealProps1 = _commonDealForProps10.dealProps1;

  return mergeProps(_objectSpread({
    lovCode: field.lovCode,
    style: {
      width: '100%'
    },
    textField: getDisplayFieldCode(field)
  }, dealProps1), contextProps);
} // 获取组件属性

/**
 * colLayout - Col 的属性
 */


var colLayout = {
  1: {
    span: 24
  },
  2: {
    span: 12
  },
  3: {
    span: 8
  },
  4: {
    span: 6
  }
};
/**
 * getColLayout - 获取字段布局 包含对应的 style
 * @param {Number} [col=3] 每行 列数
 * @param {Number} colspan 字段宽度
 */

export function getColLayout() {
  var col = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 3;
  var colspan = arguments.length > 1 ? arguments[1] : undefined;

  var colL = _cloneDeep(colLayout[col] || colLayout[3]);

  if (_isInteger(colspan) && colspan > 1) {
    colL.span = _min([24, colL.span * colspan]);
  }

  return colL;
}

function getDatePickerFormat(field, defaultFormat) {
  var format = defaultFormat;

  _forEach(field.props, function (prop) {
    if (prop.attributeName === 'format') {
      format = prop.attributeValue;
      return false;
    }
  });

  return format;
}
/**
 * 从组件的 onChange return String 给 Form 表单
 * @param dateMoment
 * @returns {*}
 */


function dateGetValueFromEventFunc(dateMoment) {
  return dateMoment && dateMoment.format(DEFAULT_DATE_FORMAT);
}
/**
 * 从组件的 onChange return String 给 Form 表单
 * @param dateMoment
 * @returns {*}
 */


function timeGetValueFromEventFunc(timeMoment) {
  return timeMoment && timeMoment.format(DEFAULT_TIME_FORMAT);
}
/**
 * 从 Form 表单 将 String 转为 moment 给组件
 * @param dateStr
 * @returns {{value: (*|moment.Moment)}}
 */


function dateGetValuePropFunc(dateStr) {
  return {
    value: dateStr && moment(dateStr, DEFAULT_DATE_FORMAT)
  };
}
/**
 * 从 Form 表单 将 String 转为 moment 给组件
 * @param dateStr
 * @returns {{value: (*|moment.Moment)}}
 */


function timeGetValuePropFunc(dateStr) {
  return {
    value: dateStr && moment(dateStr, DEFAULT_TIME_FORMAT)
  };
}
/**
 * Form 字段 获取 getValueProp 属性
 * @param field
 * @returns {*}
 */


export function getGetValuePropFunc(field) {
  switch (field.componentType) {
    case 'DatePicker':
      return dateGetValuePropFunc;

    case 'TimePicker':
      return timeGetValuePropFunc;

    default:
      return undefined;
  }
}
/**
 * Form 字段 获取 getValueFromEvent 属性
 * @param componentType
 * @returns {*}
 */

export function getGetValueFromEventFunc(componentType) {
  switch (componentType) {
    case 'DatePicker':
      return dateGetValueFromEventFunc;

    case 'TimePicker':
      return timeGetValueFromEventFunc;

    default:
      return undefined;
  }
}
/**
 * 从 dataSource 获取初始值
 * @param {*} field - 字段
 * @param {*} dataSource - 数据源
 */

export function getInitialValue(_ref) {
  var field = _ref.field,
      _ref$dataSource = _ref.dataSource,
      dataSource = _ref$dataSource === void 0 ? {} : _ref$dataSource;
  var v = dataSource[field.fieldCode];

  switch (field.componentType) {
    case 'Switch':
    case 'Checkbox':
      if (typeof v === 'string') {
        return +v ? 1 : 0;
      } else {
        return v || 0;
      }

    // return dataSource[field.fieldCode] || 0;

    default:
      return v;
  }
}
/**
 * 获取组件的类型
 * @param {*} field - 字段
 */

export function getComponentType(field) {
  switch (field.componentType) {
    case 'InputNumber':
      return components.InputNumber;

    case 'TextArea':
      return TextArea;

    case 'DatePicker':
      return components.DatePicker;

    case 'DateTimePicker':
      return components.DatePicker;

    case 'Switch':
      return components.Switch;

    case 'Checkbox':
      return components.Checkbox;

    case 'Lov':
      return components.Lov;

    case 'ValueList':
      return components.ValueList;

    case 'Upload':
      return components.Upload;

    case 'Input':
    default:
      return components.Input;
  }
}
/**
 * 获取组件的属性
 * @param {Object} field - 字段
 * @param {String} componentType - 组件类型
 * @param {Object} context - Page的this
 */

export function getComponentProps(_ref2) {
  var _ref2$field = _ref2.field,
      field = _ref2$field === void 0 ? {} : _ref2$field,
      _ref2$componentType = _ref2.componentType,
      componentType = _ref2$componentType === void 0 ? 'Input' : _ref2$componentType,
      context = _ref2.context;
  var propFunc = propUtils[componentType];

  if (_isFunction(propFunc)) {
    return propFunc(field.props, context, field);
  } else {
    return getInputProps(field.props, context, field);
  }
}

function getDisplayValue(_ref3) {
  var field = _ref3.field,
      _ref3$dataSource = _ref3.dataSource,
      dataSource = _ref3$dataSource === void 0 ? {} : _ref3$dataSource,
      _ref3$componentProps = _ref3.componentProps,
      componentProps = _ref3$componentProps === void 0 ? {} : _ref3$componentProps;
  var format;
  var dateStr;
  var value;

  switch (field.componentType) {
    case 'ValueList':
    case 'Lov':
      value = dataSource[componentProps.displayField] || dataSource["".concat(field.fieldCode, "Meaning")] || dataSource[field.fieldCode];
      return value;

    case 'DatePicker':
      format = getDatePickerFormat(field, getDateFormat());
      dateStr = dataSource[componentProps.displayField] || dataSource[field.fieldCode];
      return dateStr && moment(dateStr, DEFAULT_DATE_FORMAT).format(format);

    case 'TimePicker':
      format = getDatePickerFormat(field, DEFAULT_TIME_FORMAT);
      dateStr = dataSource[componentProps.displayField] || dataSource[field.fieldCode];
      return dateStr && moment(dateStr, DEFAULT_TIME_FORMAT).format(format);

    case 'Checkbox':
    case 'Switch':
      value = dataSource[componentProps.displayField] || dataSource[field.fieldCode];

      if (_isNil(value)) {
        return;
      }

      return _toInteger(value) === 1 ? intl.get('cpaas.common.status.yes') : intl.get('cpaas.common.status.no');

    default:
      return dataSource[componentProps.displayField] || dataSource[field.fieldCode];
  }
}

export function renderDisabledField(_ref4) {
  var field = _ref4.field,
      dataSource = _ref4.dataSource,
      organizationId = _ref4.organizationId,
      _ref4$disableStyle = _ref4.disableStyle,
      disableStyle = _ref4$disableStyle === void 0 ? 'value' : _ref4$disableStyle,
      _ref4$componentProps = _ref4.componentProps,
      componentProps = _ref4$componentProps === void 0 ? {} : _ref4$componentProps;

  switch (field.componentType) {
    case 'Upload':
      return /*#__PURE__*/React.createElement(Upload, Object.assign({}, componentProps, {
        viewOnly: true,
        attachmentUUID: getDisplayValue({
          field: field,
          dataSource: dataSource,
          componentProps: componentProps
        }),
        organizationId: organizationId
      }));

    default:
      return /*#__PURE__*/React.createElement("div", {
        className: " compose-field-value-disabled ".concat(disableStyle === 'value' ? '' : 'ant-input ant-input-disabled')
      }, getDisplayValue({
        field: field,
        dataSource: dataSource,
        componentProps: componentProps
      }));
  }
}